package com.sunyard.pcc.column;


import com.sunyard.pcc.CodeResourceUtil;
import org.apache.velocity.VelocityContext;

import java.util.Date;
import java.util.Map;

public class CodeGenerateFactory {

    private static String url = CodeResourceUtil.url;
    private static String username = CodeResourceUtil.userName;
    private static String passWord = CodeResourceUtil.password;

    private static String buss_package = CodeResourceUtil.basePackage;
    private static String projectPathCommon = getProjectPathCommon();
    private static String projectPathCore = getProjectPathCore();
    private static String projectPathDal = getProjectPathDal();
    private static String projectPathWeb = getProjectPathWeb();

    private static String getProjectPathCommon() {
        String path = System.getProperty("user.dir").replace("\\", "/") + "/";
        return path + "pcc-common/";
    }
    private static String getProjectPathCore() {
        String path = System.getProperty("user.dir").replace("\\", "/") + "/";
        return path + "pcc-core/";
    }
    private static String getProjectPathDal() {
        String path = System.getProperty("user.dir").replace("\\", "/") + "/";
        return path + "pcc-dal/";
    }
    private static String getProjectPathWeb() {
        String path = System.getProperty("user.dir").replace("\\", "/") + "/";
        return path + "pcc-web/";
    }

    /**
     * @param tableName：表名
     * @param codeName：注释
     * @param entityPackage：实体包
     * @param keyType：主键生成方式  01:UUID  02:自增
     * @param isGenerateJsp:是否生产jsp页面和js文件
     * @param isJspSimple:是否产生edit.jsp(编辑)和look.jsp(查看)
     */
    public static void codeGenerate(String tableName, String codeName, String entityPackage, String keyType,boolean isGenerateJsp,boolean isJspSimple) {
        CreateBean createBean = new CreateBean();
        createBean.setMysqlInfo(url, username, passWord);

        String className = createBean.getTablesNameToClassName(tableName);
        String lowerName = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());

        //dhcs-common
        String srcPathCommon = projectPathCommon + CodeResourceUtil.sourceBootPackage + "\\";
        String resourcePathCommon = projectPathCommon + CodeResourceUtil.resourceBootPackage + "\\";

        String pckPathCommon = srcPathCommon + CodeResourceUtil.basePackageUrl + "\\";
        String pckResourcePathCommon = resourcePathCommon + CodeResourceUtil.basePackageUrl + "\\";

        String webPath = projectPathCommon + CodeResourceUtil.webRootPackage + "\\WEB-INF\\views\\" + CodeResourceUtil.basePackageUrl + "\\";
        //dhcs-core
        String srcPathCore = projectPathCore + CodeResourceUtil.sourceBootPackage + "\\";
        String resourcePathCore = projectPathCore + CodeResourceUtil.resourceBootPackage + "\\";

        String pckPathCore = srcPathCore + CodeResourceUtil.basePackageUrl + "\\";
        String pckResourcePathCore = resourcePathCore + CodeResourceUtil.basePackageUrl + "\\";

        String webPathcore = projectPathCore + CodeResourceUtil.webRootPackage + "\\WEB-INF\\views\\" + CodeResourceUtil.basePackageUrl + "\\";
        //dhcs-dal
        String srcPathDal = projectPathDal + CodeResourceUtil.sourceBootPackage + "\\";
        String resourcePathDal = projectPathDal + CodeResourceUtil.resourceBootPackage + "\\";

        String pckPathDal = srcPathDal + CodeResourceUtil.basePackageUrl + "\\";
        String pckResourcePathDal = resourcePathDal +  "\\";

        String webPathDal = projectPathDal + CodeResourceUtil.webRootPackage + "\\WEB-INF\\views\\" + CodeResourceUtil.basePackageUrl + "\\";

        String modelPath = "page\\" + entityPackage + "\\" + className + "Page.java";
        String beanPath = "entity\\" + entityPackage + "\\" + className + "DO.java";
        String mapperPath = "dao\\" + entityPackage + "\\" + className + "Dao.java";
        String servicePath = "core\\service\\" + entityPackage + "\\" + className + "Service.java";
        String serviceImplPath = "core\\service\\impl\\" + entityPackage + "\\" + className + "ServiceImpl.java";
        String controllerPath = "controller\\" + entityPackage + "\\" + className + "Controller.java";
        String sqlMapperPath = "mapper\\" + entityPackage + "\\" + className + "Mapper.xml";
        webPath = webPath + entityPackage + "\\";

        String jspPath = "list.jsp";
        String editJspPath = "edit.jsp";
        String lookJspPath = "look.jsp";

        VelocityContext context = new VelocityContext();
        context.put("className", className);
        context.put("lowerName", lowerName);
        context.put("codeName", codeName);
        context.put("tableName", tableName);
        context.put("basePackage", buss_package);
        context.put("entityPackage", entityPackage);
        context.put("keyType", keyType);
        context.put("user", System.getProperty("user.name"));
        context.put("time", new Date());
        context.put("organization",CodeResourceUtil.getOrganization());

        try
        {
            context.put("feilds", createBean.getBeanFeilds(tableName));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try
        {
            Map sqlMap = createBean.getAutoCreateSql(tableName);
            context.put("columnDatas", createBean.getColumnDatas(tableName));
            context.put("SQL", sqlMap);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        CommonPageParser.WriterPage(context, "DoTemplate.ftl", pckPathDal, beanPath);
        CommonPageParser.WriterPage(context, "VoTemplate.ftl", pckPathDal, modelPath);
        CommonPageParser.WriterPage(context, "DaoTemplate.ftl", pckPathDal, mapperPath);
        CommonPageParser.WriterPage(context, "ServiceTemplate.ftl", pckPathCore, servicePath);
        CommonPageParser.WriterPage(context, "ServiceImplTemplate.ftl", pckPathCore, serviceImplPath);
        CommonPageParser.WriterPage(context, "MapperTemplate.xml", pckResourcePathDal, sqlMapperPath);
//        CommonPageParser.WriterPage(context, "ControllerTemplate.ftl", pckPath, controllerPath);
        if(isGenerateJsp){
            if(isJspSimple){
                CommonPageParser.WriterPage(context, "simpleJspTemplate.ftl", webPath, jspPath);
            }else{
                CommonPageParser.WriterPage(context, "jspTemplate.ftl", webPath, jspPath);
                CommonPageParser.WriterPage(context, "editJspTemplate.ftl", webPath, editJspPath);
                CommonPageParser.WriterPage(context, "lookJspTemplate.ftl", webPath, lookJspPath);
            }
        }

    }
}
